package ducpham.rogueconsole.mark2;

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Image;

public class ConsoleTile extends ATile {

	private Color background = null;
	private Image image = null;
	private CharacterSpriteCanvas csc;

	public ConsoleTile(int x, int y, CharacterSpriteCanvas csc) {
		super(x, y, csc.getTileWidth(), csc.getTileHeight());
		this.csc = csc;
	}

	@Override
	public void render(Graphics2D g) {
		super.render(g);

		if (background != null) {
			g.setColor(background);
			g.fillRect(super.getX(), super.getY(), super.getWidth(),
					super.getHeight());
		}

		if (image != null) {
			g.drawImage(image, super.getX(), super.getY(), null);
		}
	}

	@Override
	public boolean change(int param, Object arg) {
		if (param == ATile.PARAM_CHAR) {
			changeChar(arg);
			super.change(param, arg);
		}
		
		if (param == ATile.PARAM_COLOR) {
			changeColor(arg);
			super.change(param, arg);
		}
		
		return super.isChanged();
	}
	
	private void changeChar(Object arg){
		String character = null;
		try {
			character = (String) arg;
		} catch (Exception e) {
		}
		image = csc.getSprite(character);
	}
	
	private void changeColor(Object arg){
		try {
			background = (Color) arg;
		} catch (Exception e) {
			background = null;
		}
	}
	
	public Color getBackground() {
		return background;
	}
}
